package com.solt.prism.dao.imp;

import java.net.MalformedURLException;
import java.net.URL;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;

import com.solt.prism.core.entity.News;
import com.solt.prism.core.similar.DocSimilar;
import com.solt.prism.dao.CategoriesDao;
import com.solt.prism.dao.JosContent;

@Deprecated
public class JosContentImp implements JosContent {

	private static Logger logger = Logger.getLogger(JosContentImp.class);
	public static final String ATTRIBS = "show_title=\n" + "link_titles=\n"
			+ "show_intro=\n" + "show_section=\n" + "link_section=\n"
			+ "show_category=\n" + "link_category=\n" + "show_vote=\n"
			+ "show_author=\n" + "show_create_date=\n" + "show_modify_date=\n"
			+ "show_pdf_icon=\n" + "show_print_icon=\n" + "show_email_icon=\n"
			+ "language=keyref=\n" + "readmore=\n";

	public static final String INSERT_NEWS_SQL = "INSERT INTO jos_content(title,alias,introtext,`fulltext`,sectionid,catid,created,created_by,urls,ordering,state,images,attribs,metakey,metadesc,metadata) "
			+ " VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";;

	private JosContentImp() throws SQLException {
	}
	
	@Override
	public int insertNews(News news, List<DocSimilar> relateNews) {
		// TODO Auto-generated method stub
		return 0;
	}

	public static JosContentImp getJosContent() throws SQLException {
		return new JosContentImp();
	}
	
	@Override
	public int insertNews(News news, int[] relateNews) {
		insertNews(news);
		return 0;
	}

	@Override
	public int insertNews(News news) {
		Connection connection = null;
		PreparedStatement insertPre = null;
		try {
			connection = SqlConnectionPool.getConnection();
			insertPre = connection.prepareStatement(INSERT_NEWS_SQL);

			insertPre.setString(1, news.getTitle());
			insertPre.setString(2, news.getTitle());
			insertPre.setString(3, news.getDescription());
			insertPre.setString(4, news.getHtmlContent());
			if (news.getCatId() != null) {
				Integer sectionId = CategoriesDao.getInstance(false).getSetionIdOfCatId(
						news.getCatId());
				if (sectionId != null) {
					insertPre.setInt(6, news.getCatId());
					insertPre.setInt(5, sectionId);
				} else {
					logger.error("Section was null!");
					return -1;
				}
					
			} else {
				logger.error("category was null");
				return -1;
			}
			if (news.getCreateTime() == null) {
				news.setCreateTime(new Date(System.currentTimeMillis()));
			}
			insertPre.setDate(7, new Date(news.getCreateTime().getTime()));
			insertPre.setInt(8, 62);
			if (news.getSourceUrl() == null) {
				news.setSourceUrl(new URL(""));
			}
			insertPre.setString(9, news.getSourceUrl().toString());
			insertPre.setInt(10, 1);
			insertPre.setInt(11, 1);
			insertPre.setString(12, "");
			insertPre.setString(13, ATTRIBS);
			insertPre.setString(14, "");
			insertPre.setString(15, "");
			insertPre.setString(16, "");
			System.out.println("inserted db!");
			insertPre.executeUpdate();
			insertPre.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (insertPre != null)
					insertPre.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}

			try {
				if (connection != null)
					connection.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}

		}

		return 0;

	}

	public static void main(String[] args) throws SQLException {
		News news = new News();
		news.setTitle("1");
		news.setDescription("1");
		news.setCatId(1);
		news.setTextContent("1");
		news.setSectionId(1);
		news.setCreateTime(new Date(System.currentTimeMillis()));
		JosContent inserter = new JosContentImp();
		inserter.insertNews(news);

	}

}
